<?php
$loader->requireOnce("includes/Datasource_sql.class.php");

class PatientChronicDisease extends ORDataObject {
	var $patient_chronic_disease_id = 	'';
	var $patient_id 				= 	'';
	var $chronic_disease_id			=	'';
	var $date_admitted				=	'';
	var $updated 					= '';
	var $note 						= '';
	var $status 					= '';

	var $_table 			= 'patient_chronic_disease';
	var $_key 				= 'patient_chronic_disease_id';
	var $_internalName 		= 'PatientChronicDisease';
	var $_foreignKeyList	=	array(
		'chronic_disease_id'	=>	"chronic_disease",
		'patient_id'			=>	"patient"
	);

	const CHRDIS_STATUS_INSTREATMENT	=	'in_treatment';
	const CHRDIS_STATUS_RECOVERED		=	'recovered';

	function PatientChronicDisease($db='') {
		parent::ORDataObject($db);
	}

	function getPatientChronics($patient_id){
		$field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$ds 	= 	new Datasource_sql();
		$sql 	= 	array(
			'cols' 		=> "patient_chronic_disease_id as id, t1.chronic_disease_id, t2.$field as disease_name, date_admitted,
 							status, note, updated",
			'from' 		=> "$this->_table t1 
			                 LEFT JOIN ".$this->_foreignKeyList['chronic_disease_id']. " t2 ON t1.chronic_disease_id=t2.chronic_disease_id",							
							
			'where' 	=> "patient_id = ".(int)$patient_id
			);

		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}

	public function checkDuplicated($patientid,$disease_id,$admitted,$status){
		$ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "patient_chronic_disease_id  as id",
            'from'	=> "$this->_table",
        	'where'	=> "date_admitted='$admitted' AND patient_id=$patientid
        			 AND chronic_disease_id=$disease_id AND status='$status'"
            );
        $ds->setup($this->_db,$sql);
        $res	=	$ds->toArray();
      	if(!empty($res))
      		return $res[0]['id'];
      	return 0;
	}

	public function setData($patientid,$disease_id,$admitted,$status,$note,$updated=''){
		if($updated!='')
			$date=$updated;
		else
			$date=date('Y-m-d');
		$data = array (
				'patient_id' 		=> 	$patientid,
				'chronic_disease_id'=> 	$disease_id,
		        'date_admitted'     => 	$admitted,
				'note'				=> 	$note,
				'status'			=>	$status,
				'updated'			=>	$date
			);
		$this->populateArray($data);
	}

	public function getUpdated(){
		return $this->getLocalizedDate('updated');
	}

	public function getAdmitted(){
        return $this->getLocalizedDate('date_admitted');
	}

	public function getStatus(){
		$list	=	self::getChronicDiseaseStatusList();
		if(!array_key_exists($this->status, $list)){
			return '';
		}
		return $list[$this->status];
	}

	public function getNote(){
		return $this->note;
	}

	public function getDiseaseName(){
		$field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$ds =& new Datasource_sql();
		$sql = array(
            'cols'     => "t.$field as name",
            'from'     => $this->_foreignKeyList['chronic_disease_id']. " t",             
        	'where'		=>"chronic_disease_id=".(int)$this->chronic_disease_id
        );
        $ds->setup($this->_db,$sql);
        $res	=	$ds->toArray();
        if(!empty($res))
        	return $res[0]['name'];
        return '';
	}

	public function getChronicDiseaseStatusList(){
		$list	=	array(
			self::CHRDIS_STATUS_INSTREATMENT	=>	L10nHelper::_(self::CHRDIS_STATUS_INSTREATMENT),
    		self::CHRDIS_STATUS_RECOVERED		=> 	L10nHelper::_(self::CHRDIS_STATUS_RECOVERED)
    	);
        return $list;
	}

	public function deleteChronicDisease($id){
		$sql	=	"DELETE from  $this->_table WHERE $this->_key=".(int)$id;
		$this->_execute($sql);
	}

	public function setId($id){
		$this->patient_chronic_disease_id	=	$id;
	}

	public function getId(){
		return $this->patient_chronic_disease_id;
	}
}
?>